const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [],
    load: true,
    listArr: [],
    listArr2: [],
    carts : []
  },
  getcantext(){
    var that = this;
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/can/selectall',
      method: 'POST',
      header: { 'content-type': 'application/json' },
      success: res => {
            var resData = res.data;
            that.setData({
            listArr: resData.data
        })
      }
    })
  },
  onLoad() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    }),
    this.getcantext();
    this.getFoodALL();
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    var that =this;
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  getFoodALL(){
    var that = this;
    wx.request({
      url: 'https://www.woodymidnight.cn/BCUHelper/api/food/selecyall',
      method: 'POST',
      header: { 'content-type': 'application/json' },
      success: res => {
            var resData = res.data;
            that.setData({
            listArr2: resData.data
        })
      }
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;     
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  },
  creattask(){

    if (app.data.userInfo.userid == null) {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }else{
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/list/insertcanlist',
        method:'POST',
        header:{'content-type':'application/x-www-form-urlencoded'},
        data:{
          listtype: 0,
          listtitle: this.data.listtitle,
          listtext: this.data.listtext,
          toaddress: this.data.toaddress,
          username: app.data.userInfo.username,
          userphone: this.data.userphone,
          userid: app.data.userInfo.userid,
          userimage : app.data.userInfo.userimage,
          listcost: this.data.pay,
          canlistcanid: this.data.can[this.data.canType].canid,
          canlistcan: this.data.can[this.data.canType].canname,
          canlistfood: this.data.food[this.data.foodType].foodname,
          canlistfoodid: this.data.food[this.data.foodType].foodid,
          
        },
        success:(res)=>{
          if(this.data.data == null){
            this.setData({
              listtype : '',
              listtitle : '',
              listtext : '',
              getaddress : '',
              toaddress : '',
              userphone : '',
              listcost : ''
            })
          }
          console.log(res.data);
        
        }
      })
      wx.switchTab({
        url: '/pages/index/index',
      })
      wx.showToast({
        title: '创建成功',
        icon:'success',
        duration:1500
      })
    }
  },
  Jsbox(){
        // 基于数组封装队列类
        function Queue() {
          // 属性
            this.items = []
            
          // 方法
          // 1.enqueue():将元素加入到队列中
          Queue.prototype.enqueue = element => {
            this.items.push(element)
          }
       
          // 2.dequeue():从队列中删除前端元素
          Queue.prototype.dequeue = () => {
            return this.items.shift()
          }
       
          // 3.front():查看前端的元素
          Queue.prototype.front = () => {
            return this.items[0]
          }
       
          // 4.isEmpty:查看队列是否为空
          Queue.prototype.isEmpty = () => {
            return this.items.length == 0;
          }
       
          // 5.size():查看队列中元素的个数
          Queue.prototype.size = () => {
            return this.items.length
          }
       
          // 6.toString():将队列中元素以字符串形式输出
          Queue.prototype.toString = () => {
            let resultString = ''
              for (let i of this.items){
                resultString += i + ' '
              }
              return resultString
            }
          }
  }
})